import soya
import soya.label3d
import os.path
import sys


class MFile:
    def __init__(self):
        if __name__=="__main__":
            self.mf=open(os.path.join("..","game_data","other","Sokoban.def"))
        if __name__=="Sokoban.Level":
            self.mf=open(os.path.join("game_data","other","Sokoban.def"))
        
        
    def level_amount(self):
        self.mf.seek(0,0)
        lvl_amount=0
        for line in self.mf.readlines():
            if not line.find(";LEVEL"): lvl_amount+=1
        return lvl_amount
        
    def load_level(self,lvl):
        self.mf.seek(0,0)
        level=[]
        x=0
        y=-1 ## jedna linia odchodzi na ;LEVEL
        for line in self.mf.readlines():
            
            if not line.find(";LEVEL"):
                lvl_num=line.split(" ")
                lvl_num=int(lvl_num[1])
                line=""
                
            if lvl_num==lvl:
                x=0
                for znak in line:print znak,
                line=line.rstrip("\n")
                
                for znak in line:
                    level.append([x,y,znak])
                    x+=1
                y+=1
                
        if level==[]:
            sys.stderr.write("Can't load level "+str(lvl)+"\n")
            return
        #for x in level:print x
        
        self.current_level_data=level


class Model(soya.World):
        
    def __init__(self,parent=None,model_name=None):
        soya.World.__init__(self,parent)
        #self.label=soya.label3d.Label3D(self,(str(self.x)+","+str(self.y)))
        #self.label.set_xyz(0.0,3.0,0.0)
        if model_name:
            self.model=soya.Model.get(model_name)

    #def advance_time(self,proportion):
        #self.label.text=(str(self.x)+","+str(self.y))
        #pass
    
    
class Highlight(Model):
    def __init__(self,parent=None):
        Model.__init__(self,parent,"highlight1")
        self.scale(0.7,0.0,0.7)
    def advance_time(self,proportion):
        
        Model.advance_time(self,proportion)
        
        self.rotate_y(proportion)


class MainContext(soya.World,MFile): 
    
    def __init__(self,parent=None):
        
        soya.World.__init__(self,parent)
        MFile.__init__(self)
        light = soya.Light(self)
        light.set_xyz(3.5, 5.0, -5.0)   
        
        self.paker=None
        #self.draw_level()
        self.current_level=1
        self.load_level(self.current_level) 
        
    def advance_time(self,proportion):
        soya.World.advance_time(self,0.1*proportion)
        for child in self.children:child.advance_time(proportion)
  
    def testing(self):
        #self.m1=Model(self,"cube")
        #self.level.draw()
        #self.add_child(self.level.children)
        pass
    
    def next_level(self):
        if (self.current_level+1>self.level_amount()):
            self.current_level=1
        else:self.current_level+=1
        self.load_level(self.current_level)
        self.draw_level()
        
    def clear_level(self):
        lenght=len(self.children)-1
        
        while lenght >=0:
            if type(self.children[lenght]) in [type(Model()), type(Highlight())]:
                self.remove(self.children[lenght])
            lenght-=1
        print "zostalo",len(self.children),self.children
            
    def draw_level(self):
        self.clear_level()
        #print self.current_level_data
        for x in self.current_level_data:
            if x[2]==".":
                c=Highlight(self)
                c.x=x[0]*2
                c.z=x[1]*2
                #self.append(c)
            if x[2]=="#":
                c=Model(self,"mur")
                c.x=x[0]*2
                c.z=x[1]*2
            if x[2]=="$":
                c=Model(self,"cube")
                c.x=x[0]*2
                c.z=x[1]*2

        
class LevelManager:
    
    def __init__(self):
        
        pass
        
        
    def load_level(self,level_num):
        pass
    
    def clear_scene(self):
        pass
        
    def draw(self):
        pass            

       

    
        

    
    
